﻿/****************************************************************************
 * 模    块	：序列化工具类。
 * 作    者	：Jasper
 * 编写时间	：2009-3-10
 * 实现功能	：对Object实现2种序列化和反序列化。
 * 详细描述	：进行XML序列化和反序列化、进行二进制序列化和反序列化。
 ****************************************************************************/

namespace Basic4Net.Utility.Tools
{
	using System;
	using System.IO;
	using System.Xml;
	using System.Xml.Serialization;
	using System.Runtime.Serialization.Formatters.Binary;

    /// <summary>
    /// Serialize 的摘要说明。
    /// </summary>
    public class SerializeTools
    {

        /// <summary>
        /// 将对象序列化成XML字符串
        /// </summary>
        /// <param name="obj">源对象</param>
        /// <returns>XML字符串</returns>
        public static string ObjectToXmlString(object obj)
        {
            if (obj == null)
                return "";

            MemoryStream memStream = new MemoryStream();
            XmlSerializer packageSerializer = new XmlSerializer(obj.GetType());
            packageSerializer.Serialize(memStream, obj);

            StreamReader sr = new StreamReader(memStream);
            sr.BaseStream.Position = 0;

            string s = sr.ReadToEnd();

            int i = s.Length;
            return s;
        }

        /// <summary>
        /// 将XML字符串反序列化为对象
        /// </summary>
        /// <param name="s">XML字符串</param>
        /// <param name="type">反序列化为对象类类型</param>
        /// <returns>对象</returns>
        public static object XmlStringToObject(string s, System.Type type)
        {
            if (s == null || s.Length == 0)
                return null;

            MemoryStream memStream = new MemoryStream();

            StreamWriter sw = new StreamWriter(memStream);

            sw.Write(s);
            sw.Flush();
            memStream.Position = 0;

            XmlSerializer packageSerializer = new XmlSerializer(type);

            object obj = packageSerializer.Deserialize(memStream);

            return obj;
        }


        /// <summary>
        /// 2进制序列化对象
        /// </summary>
        /// <param name="obj">源对象</param>
        /// <returns>2进制数据</returns>
        public static byte[] ObjectToByte(object obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            formatter.Serialize(memStream, obj);

            memStream.Position = 0;
            byte[] data = new byte[(int)memStream.Length];

            memStream.Read(data, 0, data.Length);

            return data;

        }

        /// <summary>
        /// 2进制数据反序列化为对象
        /// </summary>
        /// <param name="data">2进制数据</param>
        /// <returns>对象</returns>
        public static object ByteToObject(byte[] data)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            memStream.Write(data, 0, data.Length);
            memStream.Position = 0;

            return formatter.Deserialize(memStream);
        }
    }
}
